home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8379 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.4 KB

  1. Path: castle.nando.net!news
  2. From: actuary@nando.net   (Bill McCarthy)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: more problems with qsort
  5. Date: 3 Mar 1996 21:19:41 GMT
  6. Organization: Nando.net Public Access
  7. Message-ID: <4hd2dd$82q@castle.nando.net>
  8. References: <177399702S86.JW1675A@american.edu> <4h0j9e$ng5@clarknet.clark.net> <4h8bud$1vd@castle.nando.net> <825783387snz@genesis.demon.co.uk>
  9. Reply-To: actuary@nando.net (Bill McCarthy)
  10. NNTP-Posting-Host: grail1506.nando.net
  11. X-Newsreader: IBM NewsReader/2 v1.2
  12.  
  13. In <825783387snz@genesis.demon.co.uk>, Lawrence Kirby <fred@genesis.demon.co.uk> writes:
  14. >In article <4h8bud$1vd@castle.nando.net>
  15. >           actuary@nando.net "Bill McCarthy" writes:
  16. >
  17. >>In <4h0j9e$ng5@clarknet.clark.net>,
  18. >>yom@clark.net (yom) writes:
  19. >>
  20. >>>Since you're trying to sort a char**, the qsort function call must
  21. >>>look like this:
  22. >>>
  23. >>>qsort(array,lines,sizeof(char **),(int (*)(void *,void *)) compare);
  24. >>>
  25. >>>And your compare function must be defined like this:
  26. >>>
  27. >>>int compare(char **a,char **b) {return strcmp(*a,*b);}
  28. >>
  29. >>Didn't you mean to type "sizeof( char * )" which, IMHO, could be
  30. >>better expressed as "sizeof array[0]" ?  Also, there's no need to
  31. >>further complicate the call of qsort with the cast on compare if
  32. >>you define compare() as:
  33. >
  34. >The code as written requires a diagnostic from the compiler (if stdlib.h
  35. >is included) because the comparison function type takes const void * arguments
  36. >and  int (*)(void *, void *)  is incompatible with
  37. >int (*)(const void *, const void *).
  38. >
  39. >Even if the cast is fixed the code results in undefined behaviour since
  40. >an int (char **, char **) function can't be legally called as an
  41. >int (const void *, const void *) function which is how qsort() will call it.
  42.  
  43. OK Lawrence, but you are replying to me.  I made two points:
  44.  
  45.   (1)  the "sizeof(char **)" should be replaced with "sizeof(char *)"
  46.   (2)  the cast in front of compare can be eliminated by rewriting compare
  47.  
  48. Which point did you disagree with?
  49.  
  50. >>int compare( const void *a, const void *b )
  51. >>{
  52. >>   return strcmp( *(const char **)a, *(const char **)b );
  53. >>}
  54. >
  55. >IMHO it is desirable to maintain corresponding consts where possible when
  56. >casting so better would be:
  57. >
  58. >    return strcmp( *(char *const *)a, *(char *const *)b );
  59. >
  60. >or
  61. >
  62. >    return strcmp( *(const char *const *)a, *(const char *const *)b );
  63.  
  64. Thanks for catching my typo.  Your second alternative is the one
  65. I've been using.
  66.  
  67. Bill McCarthy
  68. actuary@nando.net
  69. Wendell, NC  USA
  70.  
  71.